package cn.sinjet.adapter;

import java.util.List;

import com.mgroup.wazehelper.R;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.sinjet.phone.CallLog;
/**
 * @author Kevin
 */
public class CallLogAdapter extends BaseAdapter {
	private Context mContext;
	private List<CallLog> mList = null;
	private LayoutInflater inflater;
	public CallLogAdapter(List<CallLog> list, Context context) {
		mList = list;
		mContext = context;
		inflater = LayoutInflater.from(mContext);	
	}

	public void setListItem(List<CallLog> list){
		mList = list;
	}
	@Override
	public int getCount() {
		if(mList != null)
		return mList.size();
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if(mList == null || position > mList.size())
			return null;
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.phone_calllog_item, null);
			holder = new ViewHolder();
			holder.calllogType = (ImageView)convertView.findViewById(R.id.calllog_type);
			holder.name = (TextView)convertView.findViewById(R.id.result_name);
			holder.number = (TextView) convertView.findViewById(R.id.result_address);
			convertView.findViewById(R.id.phone_calllog_call).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					onClkCall(holder.number.getText().toString());
				}
			});
			convertView.setTag(holder);
//			con
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		int imgLevel = 0;
		switch(mList.get(position).getType()){
		case android.provider.CallLog.Calls.OUTGOING_TYPE:
			 imgLevel = 0;
			break;
		case android.provider.CallLog.Calls.INCOMING_TYPE:
			 imgLevel = 1;
			break;
		case android.provider.CallLog.Calls.MISSED_TYPE:
			 imgLevel = 2;
			break;
		}
		
		holder.calllogType.setImageLevel(imgLevel);
		String name = mList.get(position).getName();
		String number = mList.get(position).getNumber();
		if(!name.equals(number))
		    holder.name.setText(name);
		else holder.name.setText("未知");
		    holder.number.setText(number);
		return convertView;
	}
	
	protected void onClkCall(String number) {
		// TODO Auto-generated method stub
		if(number == null || number.length() <3) return;
		 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ number));
	     mContext.startActivity(intent);
	}

	public static class ViewHolder {
		ImageView calllogType;
		TextView  name;
		TextView  number;
	}
	
}
